#include <iostream>
#include <cstdio>
#include <unistd.h>
#include <fcntl.h>

void SetNonBlock(int fd)
{
    int fl = fcntl(fd, F_GETFL);
    if (fl < 0)
    {
        perror("fcntl");
        return;
    }
    fcntl(fd, F_SETFL, fl | O_NONBLOCK);
}

int main()
{
    SetNonBlock(0);

    char buffer[1024];
    while (true)
    {
        ssize_t n = read(0, buffer, sizeof(buffer));
        if (n > 0)
        {
            buffer[n - 1] = 0;
            std::cout << buffer << std::endl;
        }
        else if (n < 0)
        {
            if(errno == EAGAIN || errno == EWOULDBLOCK)
            {
                std::cout << "数据还没准备好..." << std::endl;
                sleep(1);
            }
            else if(errno == EINTR)
            {
                continue;
            }
            else
            {
                // 真正的错误
            }
        }
        else
        {
            break;
        }
        // sleep(1);
        // std::cout << "." << std::endl;
    }

    return 0;
}